package com.example.yl.hsh;

import android.support.test.InstrumentationRegistry;
import android.support.test.runner.AndroidJUnit4;
import android.support.test.uiautomator.By;
import android.support.test.uiautomator.UiDevice;
import android.support.test.uiautomator.UiObject2;
import android.support.test.uiautomator.Until;

import org.junit.Before;
import org.junit.FixMethodOrder;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.MethodSorters;

import java.util.logging.Level;
import java.util.logging.Logger;

@RunWith(AndroidJUnit4.class)
@FixMethodOrder(value = MethodSorters.NAME_ASCENDING)
public class ExampleInstrumentedTest {

    private long timeout = 1000 * 10;
    private UiDevice mDevies;
    private Logger logger;
    private UiObject2 funcation_1;
    private UiObject2 funcation_2;

    @Before
    public void init() {
        logger = Logger.getLogger(ExampleInstrumentedTest.class.getName());
        mDevies = UiDevice.getInstance(InstrumentationRegistry.getInstrumentation());
        mDevies.pressHome();
        mDevies.pressHome();
        UiObject2 setting = mDevies.findObject(By.textContains("四川移动和"));
        setting.click();
        try {
            Thread.sleep(3000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }

    //首页
    @Test
    public void testA_start() {
        kjFind("订流量", "流量订购");
        kjFind("花金币", "花金币");
        kjFind("电玩城", "和生活");
        kjFind("至尊会员", "我的会员");
        kjFind("星级服务", "四川移动星级服务专区");
        kjFind("建设银行", "首页");
        kjFind("和信用分", "和信用分");
        kjFind("乐乐医", "乐乐医");
        kjFind("熊猫美食", "和生活熊猫美食");
        //kjFind("更多", "流量订购");
    }

    //生活
    @Test
    public void testB_life() {
        funcation_1 = mDevies.wait(Until.findObject(By.textContains("生活")), timeout);
        if (funcation_1 != null) {
            funcation_1.click();
            kjFind("签证通", "www.visaplatform");
            kjFind("蜀景畅游", "蜀景畅游");
            kjFind("火车票", "和生活火车票");
            kjFind("驾车助理", "驾车助理");
            kjFind("车主管家", "车辆违章处理");
            kjFind("洗车易", "车友助理");
            kjFind("汽车票", "汽车订票");
            kjFind("去加油", "去加油");
            kjFind("摄影部落", "创造摄影价值");
            funcation_1 = mDevies.wait(Until.findObject(By.textContains("民生健康")), timeout);
            if (funcation_1 != null) {
                funcation_1.click();
                kjFind("享动健身", "享动就动");
                kjFind("水电气查缴", "");//弹出对话框
                kjFind("乐乐学", "乐乐学");
                kjFind("乐乐医", "乐乐医");
                kjFind("预约挂号", ".");
                kjFind("社保服务", "登录");
                kjFind("电影荟", "电影荟");
                kjFind("和小宝", "和小宝爱心助农");
                kjFind("熊猫美食", "和生活熊猫美食");
                kjFind("建设银行", "建设银行");
                kjFind("和掌柜", "和掌柜");
            } else {
                logger.log(Level.FINE, "民生健康   失败");
            }
        } else {
            logger.log(Level.FINE, "生活   失败");
        }
    }

    //我的
    @Test
    public void testC_my() {
        funcation_1 = mDevies.wait(Until.findObject(By.textContains("我的")), timeout);
        if (funcation_1 != null) {
            funcation_1.click();
            kjFind("准星", "四川移动星级服务专区");
            kjFind("流量券", "享卡券");
            kjFind("免费义诊", "乐乐医");
            kjFind("我的流量", "我的流量");
            kjFind("我的话费", "我的话费");
            kjFind("赠费记录", "移动生活");
            mDevies.swipe(100, 1500, 100, 800, 200);
            kjFind("139邮箱", "中国移动139");
            kjFind("我的相册", "相册备份");
            kjFind("我的通讯录", "通讯录备份");
            kjFind("我的彩印", "我的彩印DIY");
            kjFind("我的副号", "和多号");
            //kjFind("4G智能提速", "");
            kjFind("智能短信", "智能短信");
            kjFind("和信用分", "和信用分");
        }
    }

    /**
     * @param name  入口名称
     * @param name2 功能标识
     */
    public void kjFind(String name, String name2) {
        try {
            funcation_1 = mDevies.wait(Until.findObject(By.textContains(name)), timeout);
            if (funcation_1 != null) {
                funcation_1.click();
                Thread.sleep(2000);
                funcation_2 = null;
                funcation_2 = mDevies.wait(Until.findObject(By.textContains(name2)), timeout);
                logger.log(Level.INFO, "打开>>" + name + (null == funcation_2 ? "++失败" : ">>成功"));
            } else {
                logger.log(Level.FINE, name + "   失败");
            }
        } catch (Exception ex) {
            logger.log(Level.FINE, ex.getMessage());
            logger.log(Level.FINE, name + "   失败");
        }
        mDevies.pressBack();
    }
}
